<?php

/**
 * txtdb.php - simple database manager for a PHP Text DB connection
 *
 * This is a very simple little class that uses the PHP Text DB API to access
 * flat file databases with SQL
 */

self::lib('phptxtdb');

class TxtDb {
	/**
	 * constructor
	 *
	 * @param string db name
	 * @return void
	 */
	private function __construct($name){
		self::$name = $name;
	}

	/**
	 * connect
	 *
	 * @param void
	 * @return boolean success or failure
	 */
	private function connect(){
		if (self::$conn != null) return(true);
		else {
			$error = null;



			if (!self::$conn = @mysql_connect(self::$host, self::$user, self::$pass, true)){
				$error = array('error_code' => mysql_errno(), 'error_msg' => mysql_error());
			}
			if (!@mysql_select_db(self::$name, self::$conn)){
				$error = array('error_code' => mysql_errno(), 'error_msg' => mysql_error());
			}
			if ($error != null){
				self::$errors[] = $error;
				return(false);
			} else return(true);
		}
	}

	/**
	 * execute query
	 *
	 * @param string sql
	 * @return boolean success or failure
	 */
	public function query($sql){
		if (self::connect()){
			if (!self::$result = @mysql_query($sql, self::$conn)){
				// ERROR RESULT
				self::$errors[] = array(
					'error_code' => mysql_errno(),
					'error_msg' => mysql_error()
				);
				return(array(
					'sql' => $sql,
					'success' => false,
					'error_code' => mysql_errno(),
					'error_msg' => mysql_error()
				));
			} else {
				if (self::$result === true){
					// INSERT/UPDATE/DELETE RESULT
					return(array(
						'sql' => $sql,
						'success' => true,
						'affected_rows' => self::affected_rows(),
						'insert_id' => self::insert_id()
					));
				} else {
					// SELECT RESULT
					return(array(
						'sql' => $sql,
						'success' => true,
						'selected_rows' => self::selected_rows(),
						'rows' => self::fetch()
					));
				}
			}
		} else {
			// FAILED TO CONNECT
			return(array_merge(array('sql' => $sql), self::last_error()));
		}
	}

	/**
	 * execute safe query - this should be used instead of query() if possible
	 *
	 * i.e. $db->safe_query("select * from users where state = '%s'", "OH");
	 *
	 * @param string sql
	 * @param array vars
	 * @return boolean success or failure
	 */
	public function safe_query($sql){
		$args = func_get_args();
		array_shift($args);
		for ($i = 0; $i < count($args); $i++){
			$args[$i] = mysql_real_escape_string($args[$i], self::$conn);
		}
		return(self::query(sprintf($sql, $args)));
	}

	/**
	 * get number of affected rows from insert, update or delete command
	 *
	 * @param void
	 * @return int affected row count
	 */
	public function affected_rows(){
		return(@mysql_affected_rows(self::$conn));
	}

	/**
	 * get number of selected rows from select command
	 *
	 * @param void
	 * @return int selected row count
	 */
	public function selected_rows(){
		return(@mysql_num_rows(self::$result));
	}

	/**
	 * get the last auto incremented id from insert command
	 *
	 * @param void
	 * @return integer id
	 */
	public function insert_id(){
		return(@mysql_insert_id(self::$conn));
	}

	/**
	 * fetch result from select command
	 *
	 * @param void
	 * @return array hash of database results
	 */
	public function fetch(){
		$arr = array();
		while($row = mysql_fetch_object(self::$result)) $arr[] = $row;
		mysql_free_result(self::$result);
		return($arr);
	}

	/**
	 * get all errors
	 *
	 * @param void
	 * @return array errors
	 */
	public function errors(){
		return(self::$errors);
	}

	/**
	 * get last error
	 *
	 * @param void
	 * @return array last error
	 */
	public function last_error(){
		$idx = (count(self::$errors) > 0) ? count(self::$errors) - 1 : 0;
		return(self::$errors[$idx]);
	}
}

?>
